package com.kltn.zigo.data.update;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

public class PlaceUpdate {

	// Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();
    
    // url to get all places list
    private static String url_update_place = IP.get_IP() + IP.get_path_service() + "update_place.php";

    //private static String url_insert_place = IP.get_IP() + IP.get_path_service() + "insert_place_short.php";
    
    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    String place_name = "";
    String address = "";
    String provinceID = "";
    String districtID = "";
    String place_latitude = "";
    String place_longitude = "";
    String phone_number = "";
    String fax_number = "";
    String email = "";
    String website = "";
    String open_time = "";
    String close_time = "";
    String lowest_price = "";
    String hightest_price = "";
    String description = "";
    String avatar_path = "";
    String avatar_name = "";
    String avatar_url = "";
    String place_id = "-1";
    
    public PlaceUpdate(String PlaceID
    					,String PlaceName
						,String Address
						,String DistrictID
						,String Longitude
						,String Latitude
						,String Phone
						,String Fax
						,String Email
						,String Website
						,String OpenTime
						,String CloseTime
						,String LowestPrice
						,String HightestPrice
						,String Description
						,String URLAvatar
						,String NameAvatar)
	{
    	place_id = PlaceID;
		place_name = PlaceName;
		address = Address;
		districtID = DistrictID;
		place_longitude = Longitude;
		place_latitude = Latitude;
		phone_number = Phone;
		fax_number = Fax;
		email = Email;
		website = Website;
		open_time = OpenTime;
		close_time = CloseTime;
		lowest_price = LowestPrice;
		hightest_price = HightestPrice;
		description = Description;
		avatar_url = URLAvatar;
		avatar_name = NameAvatar;
	}
    
    public void UpdatePlace()
	{
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("place_id", place_id));
        params.add(new BasicNameValuePair("place_name", place_name));
        params.add(new BasicNameValuePair("address", address));
        params.add(new BasicNameValuePair("longitude",place_longitude));
		params.add(new BasicNameValuePair("latitude", place_latitude));
		params.add(new BasicNameValuePair("district_id", districtID));
		params.add(new BasicNameValuePair("phone_number", phone_number));
		params.add(new BasicNameValuePair("fax_number", fax_number));
		params.add(new BasicNameValuePair("email", email));
    	params.add(new BasicNameValuePair("website", website));
    	params.add(new BasicNameValuePair("open_time", open_time));
    	params.add(new BasicNameValuePair("close_time", close_time));
    	params.add(new BasicNameValuePair("lowest_price", lowest_price));
    	params.add(new BasicNameValuePair("hightest_price",hightest_price));
    	params.add(new BasicNameValuePair("description",description));
    	params.add(new BasicNameValuePair("url_avatar",avatar_url));
    	params.add(new BasicNameValuePair("name_avatar",avatar_name));

        // getting JSON Object
        // Note that create place url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_update_place, "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created placex
                //Intent i = new Intent(activity.getApplicationContext(), ListPlaceActivity.class);
                //activity.startActivity(i);

                // closing this screen
                //activity.finish();
            } else {
                // failed to create place
            	
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
	}
}
